/**
 * @file owner.c
 * Betriebssysteme User/NoUser File.
 *
 * Beispiel 1
 *
 * @author Siegfried Becher 1410258035 <siegfried.becher@technikum-wien.at>
 * @date 2015/03/12
 *
 * @version 1.0
 *
 * Handles Owners of Files
 *
 */
/*
 * -------------------------------------------------- includes --
 */




#include <pwd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "owner.h"

/*
 * --------------------------------------------------- defines --
 */

/*
 * -------------------------------------------------- typedefs --
 */

/*
 * --------------------------------------------------- globals --
 */

/*
 * ------------------------------------------------- functions --
 */

/**
 *
 * \brief check_user function
 *
 * checks if files are of specific user
 *
 * \param argv list of program arguments for program name
 * \param name name and path of the current file
 * \param parms username or user-ID
 *
 * \return bool
 *
 */

bool check_user(const char * const * argv,char *name,const char *parms)
{
  char *path = name;
  const char *user = parms;
  
  char *ptr = NULL;

  unsigned int userId = 0;
  int userIdTestNum = strtol(user, &ptr, 10);
  
  struct stat buf;
  struct passwd *pwd = NULL;

  /* if user is not root and not a string and username is not found in pwdEntry
   * --> exit with error */
  if(strcmp(user, "0") != 0 && userIdTestNum == 0 && getpwnam(user) == 0)
    {
      fprintf(stderr, "%s: '%s' is not the name of a known user\n", argv[0], user);
      
      exit(3);
      return false;
    } 

  /* get INode of file */
  if(lstat(path, &buf) == -1)
    {
      /* INode of file not found */
      fprintf(stderr, "%s: lstat failed '%s': %s\n", argv[0], path, strerror(errno));
      return false;
    }
  else
    {
      /* get pwd-Entry for username and uid */
      if((pwd = getpwuid(buf.st_uid)) == NULL)
	{
	  /* username not found */
	  fprintf(stderr, "%s: user not found: %s\n", argv[0], strerror(errno));
	  return false;
	}
      else
	{
	  /* if user is user of file */
	  if(strcmp(pwd->pw_name, user) == 0)
	    {
	      return true;
	    }
	  else 
	  {
	    /* convert user-ID to INT */
	    userId = strtol(user, &ptr, 10);
	    /* if user-ID is user-ID of file */
 	    if(userId && pwd->pw_uid == userId)
	    {
	      return true;
	    }
	  }
	}
    }

  return false;
}



/**
 *
 * \brief check_nouser function
 *
 * checks if owner of file is not in the pwd-Entry
 * 
 * \param argv list of program arguments for program name
 * \param name name and path of the current file
 *
 * \return bool
 *
 */

bool check_nouser(const char * const * argv,char *name)
{
  char *path = name;

  struct stat buf;
  struct passwd *pwd = NULL;
  
  /* get INode of File */
  if(lstat(path, &buf) == -1)
    {
      /*INode of file not found */
      fprintf(stderr, "%s: lstat failed '%s': %s\n", argv[0], path, strerror(errno));
      return false;
    }
  else
    {
      /* get pwd-Entry for username and uid */
      if((pwd = getpwuid(buf.st_uid)) == NULL)
	{
	  /* username not found -> return true for print */
	  return true;
	}
    }
  return false;
}

/*
 * ================================================== eof ==
 */
